<template>
  <div class="app-container">
    <el-row>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="50px"
               :label-position="'left'">
        <el-col :span="5">
          <el-form-item label="姓名" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入姓名"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="性别" prop="gender">
            <el-select v-model="queryParams.gender" placeholder="请选择性别" clearable>
              <el-option
                v-for="dict in dict.type.sys_user_sex"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="党派" prop="party">
            <el-select v-model="queryParams.party" placeholder="请选择党派" clearable>
              <el-option
                v-for="dict in dict.type.object_democratic_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="乡镇" prop="deptId">
<!--            <treeselect v-model="queryParams.deptId" :options="deptOptions" :show-count="true" placeholder="请选择归属乡镇"/>-->
            <el-select v-model="queryParams.deptId" placeholder="请选择归属乡镇" clearable>
              <el-option
                v-for="item in townList"
                :key="item.id"
                :label="item.label"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['object:democratic:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['object:democratic:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['object:democratic:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['object:democratic:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="democraticList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主键" align="center" prop="id"/>
      <el-table-column label="姓名" align="center" prop="name"/>
      <el-table-column label="性别" align="center" prop="gender">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.gender"/>
        </template>
      </el-table-column>
      <el-table-column label="联系方式" align="center" prop="contact"/>
      <el-table-column label="民族" align="center" prop="nation"/>
      <el-table-column label="地址" align="center" prop="position"/>
      <el-table-column label="出生年月" align="center" prop="birthday" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="参加工作时间" align="center" prop="joinWorkDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.joinWorkDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="党派" align="center" prop="party">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.object_democratic_type" :value="scope.row.party"/>
        </template>
      </el-table-column>
      <el-table-column label="学历" align="center" prop="education"/>
      <el-table-column label="职务" align="center" prop="employer"/>
      <el-table-column label="本人身份" align="center" prop="identity"/>
      <el-table-column label="政治安排情况" align="center" prop="politicalArrangement"/>
      <el-table-column label="所属乡镇" align="center" prop="deptId"/>
      <el-table-column label="政治面貌" align="center" prop="politicalStatus"/>
      <el-table-column label="简介" align="center" prop="resume"/>
      <el-table-column label="照片" align="center" prop="photo" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.photo" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="党内职务" align="center" prop="partyWork"/>
      <el-table-column label="加入党派时间" align="center" prop="joinPartyDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.joinPartyDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['object:democratic:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['object:democratic:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改民主党派成员对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名"/>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="form.gender">
            <el-radio
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="归属乡镇" prop="deptId">
          <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="请选择归属乡镇"/>
        </el-form-item>
        <el-form-item label="联系方式" prop="contact">
          <el-input v-model="form.contact" placeholder="请输入联系方式"/>
        </el-form-item>
        <el-form-item label="民族" prop="nation">
          <el-input v-model="form.nation" placeholder="请输入民族"/>
        </el-form-item>
        <el-form-item label="地址" prop="position">
          <el-input v-model="form.position" placeholder="请输入地址"/>
        </el-form-item>
        <el-form-item label="出生年月" prop="birthday">
          <el-date-picker clearable
                          v-model="form.birthday"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择出生年月">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="参加工作时间" prop="joinWorkDate">
          <el-date-picker clearable
                          v-model="form.joinWorkDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择参加工作时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="党派" prop="party">
          <el-select v-model="form.party" placeholder="请选择党派">
            <el-option
              v-for="dict in dict.type.object_democratic_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学历" prop="education">
          <el-input v-model="form.education" placeholder="请输入学历"/>
        </el-form-item>
        <el-form-item label="职务" prop="employer">
          <el-input v-model="form.employer" placeholder="请输入职务"/>
        </el-form-item>
        <el-form-item label="本人身份" prop="identity">
          <el-input v-model="form.identity" placeholder="请输入本人身份"/>
        </el-form-item>
        <el-form-item label="政治安排情况" prop="politicalArrangement">
          <el-input v-model="form.politicalArrangement" placeholder="请输入政治安排情况"/>
        </el-form-item>
        <el-form-item label="政治面貌" prop="politicalStatus">
          <el-input v-model="form.politicalStatus" placeholder="请输入政治面貌"/>
        </el-form-item>
        <el-form-item label="简介">
          <editor v-model="form.resume" :min-height="192"/>
        </el-form-item>
        <el-form-item label="照片">
          <image-upload v-model="form.photo"/>
        </el-form-item>
        <el-form-item label="党内职务" prop="partyWork">
          <el-input v-model="form.partyWork" placeholder="请输入党内职务"/>
        </el-form-item>
        <el-form-item label="加入党派时间" prop="joinPartyDate">
          <el-date-picker clearable
                          v-model="form.joinPartyDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择加入党派时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listDemocratic, getDemocratic, delDemocratic, addDemocratic, updateDemocratic} from "@/api/object/democratic";
import {treeselect} from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Democratic",
  dicts: ['sys_user_sex', 'object_democratic_type'],
  components: {
    Treeselect
  },
  data() {
    return {
      townList: [],
      deptOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 民主党派成员表格数据
      democraticList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        gender: null,
        party: null,
        deptId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          {required: true, message: "姓名不能为空", trigger: "blur"}
        ],
        gender: [
          {required: true, message: "性别不能为空", trigger: "blur"}
        ],
        contact: [
          {required: true, message: "联系方式不能为空", trigger: "blur"}
        ],
        nation: [
          {required: true, message: "民族不能为空", trigger: "blur"}
        ],
        position: [
          {required: true, message: "地址不能为空", trigger: "blur"}
        ],
        party: [
          {required: true, message: "党派不能为空", trigger: "change"}
        ],
        deptId: [
          {required: true, message: "所属乡镇不能为空", trigger: "change"}
        ],
        photo: [
          {required: true, message: "照片不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getTreeselect();
  },
  methods: {
    /** 查询乡镇下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
        this.townList = response.data[0].children
      });
    },
    /** 查询民主党派成员列表 */
    getList() {
      this.loading = true;
      listDemocratic(this.queryParams).then(response => {
        this.democraticList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        gender: 0,
        contact: null,
        nation: null,
        position: null,
        birthday: null,
        joinWorkDate: null,
        party: null,
        education: null,
        employer: null,
        identity: null,
        politicalArrangement: null,
        deptId: null,
        politicalStatus: null,
        resume: null,
        photo: null,
        partyWork: null,
        joinPartyDate: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加民主党派成员";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDemocratic(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改民主党派成员";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDemocratic(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDemocratic(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除民主党派成员编号为"' + ids + '"的数据项？').then(function () {
        return delDemocratic(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('object/democratic/export', {
        ...this.queryParams
      }, `democratic_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
